﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace SoarD.Pandora
{
    public class PdrFileUtil
    {
        #region Create

        public static void CreateDirectory(string path)
        {
            path = path.Replace("\\", "/");
            string[] pathArry = path.Split('/');
            StringBuilder sb = new StringBuilder(pathArry[0]);
            for (int i = 1; i < pathArry.Length; i++)
            {
                sb.Append(string.Format("/{0}", pathArry[i]));
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
        }

        public static void SaveTextToFile(string path, string fileName, string content)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(content);
            SaveBytesToFile(path, fileName, bytes);
        }
        public static void SaveBytesToFile(string path, string fileName, byte[] bytes)
        {
            CreateDirectory(path);
            try
            {
                var filePath = Path.Combine(path, fileName);
                Stream stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite);
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
        }
        #endregion

        #region Delete

        public static void DeleteDirectory(string path)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    DirectoryInfo di = new DirectoryInfo(path);
                    di.Delete(true);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
        }

        public static void DeleteFile(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
        }
        #endregion

        #region Read

        public static string ReadTextFromFile(string path, string defaultValue = "")
        {
            string ret = defaultValue;

            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
            {
                StreamReader reader = fi.OpenText();
                ret = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
            }

            return ret;
        }
        #endregion

        #region Open
        public static void OpenFolder(string folder)
        {
            switch (Application.platform)
            {
                case RuntimePlatform.WindowsEditor:
                    Process.Start("Explorer.exe", folder.Replace('/', '\\'));
                    break;
                case RuntimePlatform.OSXEditor:
                    Process.Start("open", folder);
                    break;
            }
        }
        #endregion

        #region GetFile
        public static List<string> GetAllChildFiles(string path, string suffix = null, List<string> files = null)
        {
            if (files == null)
            {
                files = new List<string>();
            }

            AddFiles(path, suffix, files);

            string[] temps = Directory.GetDirectories(path);
            for (int i = 0; i < temps.Length; ++i)
            {
                string dir = temps[i];
                GetAllChildFiles(dir, suffix, files);
            }

            return files;
        }

        public static void AddFiles(string path, string suffix, List<string> files)
        {
            string[] temps = Directory.GetFiles(path);
            for (int i = 0; i < temps.Length; ++i)
            {
                string file = temps[i];
                if (string.IsNullOrEmpty(suffix) || file.ToLower().EndsWith(suffix.ToLower()))
                {
                    files.Add(file);
                }
            }
        }
        #endregion
    }
}